package userInterface;

import javax.swing.*; 

import staticClasses.Global;

import java.awt.*;

public class SimulationThread extends Thread {

	//local team players positions 
	private int xLocalGoal = 0; 
	private int yLocalGoal = 0;
	private int xLocalNorthBack = 0; 
	private int yLocalNorthBack = 0;
	private int xLocalSouthBack = 0; 
	private int yLocalSouthBack = 0;
	private int xLocalMid = 0; 
	private int yLocalMid = 0;
	private int xLocalNorthForward = 0; 
	private int yLocalNorthForward = 0;
	private int xLocalSouthForward = 0; 
	private int yLocalSouthForward = 0;

	//visitor team players positions
	private int xVisitorGoal = 0; 
	private int yVisitorGoal = 0;
	private int xVisitorNorthBack = 0; 
	private int yVisitorNorthBack = 0;
	private int xVisitorSouthBack = 0; 
	private int yVisitorSouthBack = 0;
	private int xVisitorMid = 0; 
	private int yVisitorMid = 0;
	private int xVisitorNorthForward = 0; 
	private int yVisitorNorthForward = 0;
	private int xVisitorSouthForward = 0; 
	private int yVisitorSouthForward = 0;
	private int xBall = 0;
	private int yBall = 0;


	private JTabbedPane fieldSectionPanel;

	public SimulationThread(JTabbedPane fieldPanel) {
		this.fieldSectionPanel = fieldPanel;
	}

	public void run() {
		JFrame cadre = new JFrame(); 
		cadre.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		MonPanneau panneau = new MonPanneau();
		fieldSectionPanel.add("Soccer Field", panneau);
		
		JLabel scoreLabel = new JLabel("<html> " +
				"<p align=\"center\"><font size=\"5\" color=\"#272F78\"><b>SCORE : "+Global.score[0]+"-"+Global.score[1]+"</font></b></p>" +
				"</html>");
		
		panneau.add(scoreLabel);
		
		while (Global.play) {
			if (!Global.pause) {
				
				scoreLabel.setText("<html> " +
						"<p align=\"center\"><font size=\"5\" color=\"#272F78\"><b>SCORE : "+Global.score[0]+"-"+Global.score[1]+"</font></b></p>" +
						"</html>");

				//local team players positions update
				xLocalGoal = 9+(Global.localTeam.get(0).getPosition())[0];
				yLocalGoal = 465-(Global.localTeam.get(0).getPosition())[1];
				xLocalNorthBack = -5+(Global.localTeam.get(1).getPosition())[0];
				yLocalNorthBack = 441-(Global.localTeam.get(1).getPosition())[1];
				xLocalSouthBack = -5+(Global.localTeam.get(2).getPosition())[0];
				yLocalSouthBack = 441-(Global.localTeam.get(2).getPosition())[1];
				xLocalMid = -5+(Global.localTeam.get(5).getPosition())[0];
				yLocalMid = 441-(Global.localTeam.get(5).getPosition())[1];
				xLocalNorthForward = -5+(Global.localTeam.get(3).getPosition())[0];
				yLocalNorthForward = 441-(Global.localTeam.get(3).getPosition())[1];
				xLocalSouthForward = -5+(Global.localTeam.get(4).getPosition())[0];
				yLocalSouthForward = 441-(Global.localTeam.get(4).getPosition())[1];

				//local team players positions update
				xVisitorGoal = 35+(Global.visitorTeam.get(0).getPosition())[0];
				yVisitorGoal = 465-(Global.visitorTeam.get(0).getPosition())[1];
				xVisitorNorthBack = 35+(Global.visitorTeam.get(1).getPosition())[0];
				yVisitorNorthBack = 441-(Global.visitorTeam.get(1).getPosition())[1];
				xVisitorSouthBack = 35+(Global.visitorTeam.get(2).getPosition())[0];
				yVisitorSouthBack = 441-(Global.visitorTeam.get(2).getPosition())[1];
				xVisitorMid = 35+(Global.visitorTeam.get(5).getPosition())[0];
				yVisitorMid = 441-(Global.visitorTeam.get(5).getPosition())[1];
				xVisitorNorthForward = 35+(Global.visitorTeam.get(3).getPosition())[0];
				yVisitorNorthForward = 441-(Global.visitorTeam.get(3).getPosition())[1];
				xVisitorSouthForward = 35+(Global.visitorTeam.get(4).getPosition())[0];
				yVisitorSouthForward = 441-(Global.visitorTeam.get(4).getPosition())[1];	

				//ball positions update
				xBall = 25+(Global.ball.getPosition())[0];
				yBall = 476-(Global.ball.getPosition())[1];

				panneau.repaint();
				try { 
					Thread.sleep(20);
				} catch(Exception ex) { 
				} 
			}
		}
	}

	class MonPanneau extends JPanel {

		private static final long serialVersionUID = 1L;

		public void paintComponent(Graphics g) {	
			g.clearRect(0, 0, this.getWidth(), this.getHeight());
			
			//paint the soccer field
			Image img = (new ImageIcon(getClass().getResource("soccer-field.jpg"))).getImage();
			g.drawImage(img, 0, 0,this);
			
			//paint the score
//			JLabel scoreLabel = new JLabel("<html> " +
//					"<p align=\"center\"><font size=\"5\" color=\"#272F78\"><b>SCORE : "+Global.score[0]+"-"+Integer.Global.score[1]+"</font></b></p>" +
//					"</html>");
//			g.drawI
			
			//paint the players of the local team
			Image imgGoal1 = (new ImageIcon(getClass().getResource("goal-keeper1.png"))).getImage();
			g.drawImage(imgGoal1, xLocalGoal, yLocalGoal,this);
			Image img11 = (new ImageIcon(getClass().getResource("player11.png"))).getImage(); 
			g.drawImage(img11, xLocalNorthBack, yLocalNorthBack,this);
			Image img21 = (new ImageIcon(getClass().getResource("player21.png"))).getImage();
			g.drawImage(img21, xLocalSouthBack, yLocalSouthBack,this);
			Image img31 = (new ImageIcon(getClass().getResource("player31.png"))).getImage();
			g.drawImage(img31, xLocalMid, yLocalMid,this);
			Image img41 = (new ImageIcon(getClass().getResource("player41.png"))).getImage();
			g.drawImage(img41, xLocalNorthForward, yLocalNorthForward,this);
			Image img51 = (new ImageIcon(getClass().getResource("player41.png"))).getImage();
			g.drawImage(img51, xLocalSouthForward, yLocalSouthForward,this);
			
			//paint the players of the visitor team
			Image imgGoal2 = (new ImageIcon(getClass().getResource("goal-keeper2.png"))).getImage();
			g.drawImage(imgGoal2, xVisitorGoal, yVisitorGoal,this);
			Image img12 = (new ImageIcon(getClass().getResource("player42.png"))).getImage();
			g.drawImage(img12, xVisitorNorthBack, yVisitorNorthBack,this);
			Image img22 = (new ImageIcon(getClass().getResource("player42.png"))).getImage();
			g.drawImage(img22, xVisitorSouthBack, yVisitorSouthBack,this);
			Image img32 = (new ImageIcon(getClass().getResource("player12.png"))).getImage();
			g.drawImage(img32, xVisitorMid, yVisitorMid,this);
			Image img42 = (new ImageIcon(getClass().getResource("player22.png"))).getImage();
			g.drawImage(img42, xVisitorNorthForward, yVisitorNorthForward,this);
			Image img52 = (new ImageIcon(getClass().getResource("player32.png"))).getImage();
			g.drawImage(img52, xVisitorSouthForward, yVisitorSouthForward,this);
			
			//paint the ball
			Image imgBall = (new ImageIcon(getClass().getResource("ball.png"))).getImage();
			g.drawImage(imgBall, xBall, yBall,this);
		
		}
	}

}